console.log("------------observer观察者模式-------------");
class Observer {
    constructor() {
        this.handle = [];//相关订阅fn
    }
    addSubscribe(topic, fn) {
        this.handle.push({ topic: topic, fn: fn });
        console.log(this.handle);
    }
    removeSubscribe(topic, fn) {
        let index;
        this.handle.forEach(function (data, i) {
            if (data.topic == topic && data.fn == fn) {
                index = i;
            }
        });
        delete this.handle[index];
        console.log(this.handle);
    }
    publish(topic, message) {
        this.handle.forEach(function (data) {
            if (data.topic == topic) {
                data.fn(message);
            }
        });
    }
}
export default Observer;
{
    let test = new Observer();
    let fn1 = function (data) {
        console.log(data);
    }
    let fn2 = function (data) {
        console.log("I'm in fn2 say " + data);
    }
    test.addSubscribe('abc', fn1);
    test.addSubscribe('abc', fn2);
    test.addSubscribe('abcd', fn2);
    test.publish('abc', 'hello');
    test.publish('abcd', 'good job');
    test.removeSubscribe('abc', fn2);
    test.publish('abc', 'fn2 you are here?')
}

console.log("--------------------------------------");
